package cn.com.shadowless.xinning.view.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.internal.LinkedTreeMap;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.ImageViewerPopupView;
import com.lxj.xpopup.interfaces.OnSrcViewUpdateListener;
import com.lxj.xpopup.util.SmartGlideImageLoader;
import com.mengpeng.mphelper.ToastUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.adapter.SupplierImgAdapter;
import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.model.order.AssetMoveInfo;
import cn.com.shadowless.xinning.model.order.SupplierInfo;
import cn.com.shadowless.xinning.presenter.OrderPresenter;
import cn.com.shadowless.xinning.utils.Constants;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.view.BaseFragment;

/**
 * The type Supplier fragment.
 *
 * @author sHadowLess
 */
public class SupplierFragment extends BaseFragment {

    /**
     * The Supplier name.
     */
    @BindView(R.id.supplier_name)
    TextView supplierName;

    /**
     * The Supplier ss code.
     */
    @BindView(R.id.supplier_ss_code)
    TextView supplierSsCode;

    /**
     * The Supplier bank.
     */
    @BindView(R.id.supplier_bank)
    TextView supplierBank;

    /**
     * The Supplier bank num.
     */
    @BindView(R.id.supplier_bankNum)
    TextView supplierBankNum;

    /**
     * The Supplier company phone.
     */
    @BindView(R.id.supplier_companyPhone)
    TextView supplierCompanyPhone;

    /**
     * The Supplier address.
     */
    @BindView(R.id.supplier_address)
    TextView supplierAddress;

    /**
     * The Supplier contact.
     */
    @BindView(R.id.supplier_contact)
    TextView supplierContact;

    /**
     * The Supplier comment.
     */
    @BindView(R.id.supplier_comment)
    TextView supplierComment;

    /**
     * The Supplier remark.
     */
    @BindView(R.id.supplier_remark)
    TextView supplierRemark;

    /**
     * The Supplier content.
     */
    @BindView(R.id.supplier_content)
    TextView supplierContent;

    /**
     * The Supplier credentials.
     */
    @BindView(R.id.supplier_credentials)
    RecyclerView supplierCredentials;

    /**
     * The Supplier check process.
     */
    @BindView(R.id.supplier_check_process)
    LinearLayout supplierCheckProcess;

    private final String TAG = SupplierFragment.class.getSimpleName();

    private String url = "api/Partners/LoadCheckHistory?%s=";

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_supplier_layout;
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        if (null != bundle) {
            String orderId = bundle.getString("orderId");
            String userName = bundle.getString("userName");
            initSupplierInfo(orderId, userName);
            initCheckHistoryList(String.format(url, "id") + orderId, mActivity, supplierCheckProcess);
        }
    }

    @Override
    protected void initView() {

    }

    private void initSupplierInfo(String orderId, String userName) {
        OrderPresenter
                .getSingleton()
                .getSupplierInfo(orderId, userName, new RequestCallBack<SupplierInfo>() {
                    @Override
                    public void success(RequestResult<SupplierInfo> result) {
                        String token = result.getLoginToken();
                        String oldToken = PreferencesUtils.getString(MyApplication.getInstance(), "token", "-1");
                        if (result.isSucceed() && null != result.getResult()) {
                            if (null != token && token.equals(oldToken)) {
                                Log.e(TAG, "success: " + result.toString());
                                SupplierInfo.DataDTO dataBean = result.getResult().getData();
                                List<Object> fileList = result.getResult().getData().getFileList();
                                List<Object> popFileList = new ArrayList<>();
                                for (Object s : fileList) {
                                    LinkedTreeMap<String, String> tree = (LinkedTreeMap<String, String>) s;
                                    popFileList.add(Constants.FILE_URL + tree.get("fileUrl"));
                                }
                                String name = dataBean.getPartnersName();
                                String ssCode = dataBean.getPartnersCode();
                                String accountBank = dataBean.getAccountBank();
                                String accountNo = dataBean.getAccountNo();
                                String contactPhone = dataBean.getContactPhone();
                                String partnersAddress = dataBean.getPartnersAddress();
                                String contactName = dataBean.getContactName();
                                String evaluate = dataBean.getEvaluate();
                                String remark = dataBean.getRemark();
                                String mainContents = dataBean.getMainContents();
                                supplierName.setText(name);
                                supplierSsCode.setText(ssCode);
                                supplierBank.setText(accountBank);
                                supplierBankNum.setText(accountNo);
                                supplierCompanyPhone.setText(contactPhone);
                                supplierAddress.setText(partnersAddress);
                                supplierContact.setText(contactName);
                                supplierComment.setText(evaluate);
                                supplierRemark.setText(remark);
                                supplierContent.setText(mainContents);
                                supplierCredentials.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.HORIZONTAL, false));
                                SupplierImgAdapter supplierImgAdapter = new SupplierImgAdapter(R.layout.fragment_supplier_item, fileList);
                                supplierImgAdapter.setOnItemChildClickListener((adapter, view, position) -> new XPopup
                                        .Builder(mActivity)
                                        .dismissOnTouchOutside(true)
                                        .asImageViewer(view.findViewById(R.id.supplier_img), position, popFileList, (popupView, position1) -> {
                                            RecyclerView rv = (RecyclerView) view.getParent();
                                            popupView.updateSrcView((ImageView) rv.getChildAt(position1));
                                        }, new SmartGlideImageLoader())
                                        .show());
                                supplierCredentials.setAdapter(supplierImgAdapter);
                                supplierImgAdapter.notifyDataSetChanged();
                            } else {
                                ToastUtils.onErrorShowToast("登录态与服务器不一致");
                                initLogoutInfo(mActivity, userName);
                            }
                        } else {
                            ToastUtils.onErrorShowToast("该订单暂无详细信息");
                        }
                    }

                    @Override
                    public void fail(String errorMsg) {
                        ToastUtils.onErrorShowToast(errorMsg);
                    }
                });
    }
}
